<?php
App::uses('AppModel', 'Model');
/**
 * ArticlesCategory Model
 *
 * @property article $article
 * @property question $question
 */
class ArticlesCategory extends AppModel {
/**
 * Primary key field
 *
 * @var string
 */
	public $primaryKey = 'id_categorie';
/**
 * Display field
 *
 * @var string
 */
	public $displayField = 'description_categorie';
/**
 * Validation rules
 *
 * @var array
 */
 
	public $validate = array(
			
		'id_categorie' => array(
			'numeric' => array(
				'rule' => array('numeric'),
				//'message' => 'Your custom message here',
				//'allowEmpty' => false,
				//'required' => false,
				//'last' => false, // Stop validation after this rule
				//'on' => 'create', // Limit validation to 'create' or 'update' operations
			),
		),
			
		'description_categorie' => array(
			'notempty' => array(
				'rule' => array('notempty'),
				'message' => 'Ce champ doit être renseigné.',
				//'allowEmpty' => false,
				//'required' => false,
				//'last' => false, // Stop validation after this rule
				//'on' => 'create', // Limit validation to 'create' or 'update' operations
			),
						'unique' => array(
							'rule' => 'isUnique',
							'message' => 'Cette catégorie existe déjà.'
							
						)
		),
	);

	public function toutesLesCategoriesEtLeursArticles()
	{
		$result = $this->query("SELECT * FROM articles_categories ".
								"JOIN articles ON articles.id_categorie = articles_categories.id_categorie ".
								"JOIN groupe_devis ON groupe_devis.id_groupe_devis = articles.id_groupe_devis ".
								"JOIN bloc_devis ON bloc_devis.id_bloc_devis = groupe_devis.id_bloc_devis");
		
		$out = array();
		
		// on met en forme de fa�on a ce que les categories contiennent les articles
		foreach ($result as $r) {
			// ajout d'une categorie si elle n'existe pas encore
			if(!isset($out[$r['articles_categories']['id_categorie']])) {
				$out[$r['articles_categories']['id_categorie']] = $r['articles_categories'];
				$out[$r['articles_categories']['id_categorie']]['articles'] = Array();
			}
			// ajout du t�l�phone pour la categorie
			// $out[$r['articles_categories']['id_categorie']]['articles'][] = $r['articles']; // version précédente, ajout dans le tableau simple
			$out[$r['articles_categories']['id_categorie']]['articles'][$r['articles']['id_article']] = $r['articles']; // nouvelle version, ajout dans le tableau avec comme indice l'id de l'article
			$out[$r['articles_categories']['id_categorie']]['articles'][$r['articles']['id_article']]['groupe'] = $r['groupe_devis'];
			$out[$r['articles_categories']['id_categorie']]['articles'][$r['articles']['id_article']]['bloc'] = $r['bloc_devis'];
		}
		
		return $out;
	}
	
	//The Associations below have been created with all possible keys, those that are not needed can be removed

/**
 * hasMany associations
 *
 * @var array
 */
	public $hasMany = array(
		'article' => array(
			'className' => 'article',
			'foreignKey' => 'id_categorie',
			'dependent' => false,
			'conditions' => '',
			'fields' => '',
			'order' => '',
			'limit' => '',
			'offset' => '',
			'exclusive' => '',
			'finderQuery' => '',
			'counterQuery' => ''
		),
		'question' => array(
			'className' => 'question',
			'foreignKey' => 'id_categorie',
			'dependent' => false,
			'conditions' => '',
			'fields' => '',
			'order' => '',
			'limit' => '',
			'offset' => '',
			'exclusive' => '',
			'finderQuery' => '',
			'counterQuery' => ''
		)
	);

}
